/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  URLAbstraction.java                                      
 *  File Contents Outline :  URL提取                  
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.filter.logfilter;

/**
 *
 * @author wan
 */
public class URLAbstraction {

    //截取url及referer规则
    public static String getPartURL(String url) {
        String temp1 = "";
        String temp2 = "";    

        int questionMarkIndex = url.indexOf("?");
        int headLength = 0;

        //对于盘古url的处理
        if (url.startsWith("http://www.panguso.com/")) {
            if (questionMarkIndex < 0) {
                return url;
            } else {
                String temp0 = url.substring(0, questionMarkIndex);
                return temp0;
            }
        } 
        
        //对于普通url的处理，截取二级域名
        else {
            //截取问号之前的内容        
            if (questionMarkIndex < 0) {
                temp2 = url.substring(url.indexOf("//") + 2);//http后的内容
                headLength = url.indexOf("//") + 2;
                temp1 = url;
            } else {
                temp1 = url.substring(0, questionMarkIndex);//问号之前的内容
                temp2 = temp1.substring(temp1.indexOf("//") + 2);
                headLength = temp1.indexOf("//") + 2;
            }

            //截取第一个/之前的内容
            int firstSplit = temp2.indexOf("/");
            if (firstSplit < 0) {
//                System.out.println("final：" + temp1);
                return temp1;
            } else {
                String temp3 = temp2.substring(firstSplit + 1);//第一个/之后的内容
                int secondSplit = temp3.indexOf("/");
                if (secondSplit < 0) {
//                    System.out.println("final:" + temp1);
                    return temp1;
                } else {
                    int secondIndex = firstSplit + secondSplit + 1;
                    String temp4 = temp1.substring(0, secondIndex + headLength);
//                    System.out.println("final:" + temp4);
                    return temp4;
                }
            }
        }
    }
}
